Ext.define('js.sysmenu.TreeList', {
    extend: 'Ext.panel.Panel',
    region: 'west',
    width: 200,
    split: false,
    id: 'treelistContainer',
    bodyCls: 'treelistContainer',
    layout: {
        type: 'vbox',
        align: 'stretch'
    },
    border: false,
    scrollable: 'y',
    initComponent: function () {
        console.log(util.getUserGroup());
        let adminTreeStore = Ext.create('js.store.sysmenu.AdminTreeListStore');
        let userTreeStore = Ext.create('js.store.sysmenu.UserTreeListStore');
        let treeListStore = util.isAdmin() ? adminTreeStore : userTreeStore;
        let treeList = {
            xtype: 'treelist',
            id: 'treelist',
            rootVisible: false,
            expanderOnly: false,
            ui: 'nav',
            style: 'background:#fff;',
            store: treeListStore,
            listeners: {
                selectionchange: function (model, selected) {
                    try {
                        if (!selected.isLeaf()) return false;
                        let SysContainer = Ext.getCmp('SysContainer');
                        let menuId = selected.get('id')||selected.get('name');
                        let tabId = ['js', menuId].join('.');
                        let tabCmp = Ext.getCmp(tabId);
                        //刷新
                        SysContainer.removeAll(true);
                        SysContainer.add(Ext.create(tabId, {id: tabId}));
                        SysContainer.getLayout().setActiveItem(tabId);
                    } catch (e) {
                        console.error(e.message);
                    }
                }
            }
        };
        this.items = [treeList];
        this.callParent(arguments);

    },
    listeners: {
        render: function () {
            let treelist = Ext.getCmp('treelist');
            treelist.setExpanderFirst(false);
            treelist.setUi('nav');
            treelist.setHighlightPath(true);
            let treeStore = treelist.getStore();
            try {
                let success = function (response) {
                        let obj = Ext.decode(response.responseText);
                    },
                    fail = function (response) {
                        let obj = Ext.decode(response.responseText);
                        Ext.toast(obj.message);
                    };
                util.get('getUserInfoBySession').then(success, fail);
            }catch(e){
                console.log(e);
            }
            treelist.setSelection(treeStore.getRoot().firstChild);
        }
    }
});
